package com.gss.plugin.oauth2.filter;

import com.gss.entity.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author: DYang
 * @since: 2023/8/23
 * @description:
 */
@Slf4j
@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    protected ResponseEntity<Object> handleException(Exception ex) {
        ex.printStackTrace();
        logger.error(ex);

        // 将异常信息转换为自定义响应
        R<Object> res = R.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作发生错误，请检查服务");
        res.setDesc(ex.getMessage());
        return new ResponseEntity<>(res, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
